<template>
  <Dialog :title="title" :visible="visible" class="single-column" button-text="保存" @close="handleClose('true')" @confirm="handleConfirm">
    <el-form ref="ruleForm" :loading="loading" :model="selectRow" :rules="rules" label-width="100px">
      <el-form-item label="名称：" prop="name">
        <el-input v-model="selectRow.name" style="width: 235px;" />
      </el-form-item>
      <!-- 只有一个医院时,不显示,默认选中当前医院 -->
      <!-- <el-form-item label="医院:" prop="hospitalName" style="width:80%">
        <el-select v-model="selectRow.hospitalName" placeholder="请选择医院">
          <el-option label="区域一" value="shanghai" />
          <el-option label="区域二" value="beijing" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="状态：" prop="status">
        <el-radio v-model="selectRow.status" label="启用" />
        <el-radio v-model="selectRow.status" label="停用" />
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<script>
import Dialog from "@/components/Dialog";
export default {
  components: { Dialog },
  data() {
    return {
      ruleForm: {},
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        hospitalName: [
          { required: true, message: '请选择医院', trigger: 'blur' },
        ],
      },
      loading: false,
      title: '增加床位',
      selectRow: {},
      visible: false,
    };
  },
  methods: {
    handleClose(number, row) {
      this.visible = true;
      if (number === 'true') {
        this.visible = false;
        this.selectRow = {};
      } else {
        number === 1 ? this.title = "增加床位" : this.title = "修改床位";
        this.selectRow = JSON.parse(JSON.stringify(row));
      }
    },
    handleConfirm() {
      this.$refs.ruleForm.validate(valid => {
        if (!valid) return;
        this.loading = true;
        if (this.selectRow.id) {
          this.$api[`department/bed/edit`]({ ...this.selectRow }).then(res => {
            if (res.code !== 200) return this.$massage.error('修改床位失败');
            this.handleClose('true');
            this.$emit("success");
          }).finally(() => (this.loading = false));
        } else {
          this.$api[`department/bed/insert`]({ ...this.selectRow }).then(res => {
            if (res.code !== 200) return this.$massage.error('增加床位失败');
            this.msgSuccess('增加床位成功');
            this.$emit("success");
            this.handleClose('true');
          }).finally(() => (this.loading = false));
        }
      });
    },
  },
};
</script>
